<?php
/**
 * Admin Setting Controller
 *
 * @package    Gleez\Controller\Admin
 * @author     Gleez Team
 * @version    1.0.1
 * @copyright  (c) 2011-2013 Gleez Technologies
 * @license    http://gleezcms.org/license  Gleez CMS License
 */
class Controller_Admin_Cache extends Controller_Admin {

    /**
     * Clear cache
     */
    public function action_clear()
    {
        $result = $this->clearDir(Kohana::$cache_dir);
        $result ? Message::success('操作成功') : Message::error('操作失败');
        $this->request->redirect(Route::get('user')->uri(array('action' => 'profile')), 200);
        $this->response->body();
    }
    
    protected function clearDir($dir) {
        $op = dir($dir);
        while(false != ($item = $op->read())) {
            if($item == '.' || $item == '..') {
                continue;
            }
            if(is_dir($op->path.'/'.$item)) {
                $this->clearDir($op->path.'/'.$item);
                @rmdir($op->path.'/'.$item);
            } else {
                $res = @unlink($op->path.'/'.$item);
                if(!$res) {
                    return false;
                }
            }
        }
        return true;
    }
}
